//1.引用系统模块
const http = require('http');
//处理URL地址
const url = require('url');
//2.创建web服务器
const app = http.createServer();
//3.当客户端请求发送请求的时候
app.on('request', (req, res) => {
    //获取请求方式req.method
    //console.log(req.method);

    //获取请求地址
    //req.url
    //console.log(req.url);

    //获取请求报文信息
    //req.headers
    //console.log(req.headers['accept']);

    res.writeHead(200, {
        'content-type': 'text/html;charset=utf8'
    });

    console.log(req.url);
    //解析URL返回对象
    let {
        query,
        pathname
    } = url.parse(req.url, true);
    console.log(query.name);
    console.log(query.age);

    if (pathname == '/index' || pathname == '/') {
        res.end('<h2>欢迎index</h2>');
    } else if (pathname == '/list') {
        res.end('list')
    } else {
        res.end('null')
    }

    if (req.method == 'POST') {
        res.end('post')
    } else if (req.method == 'GET') {
        res.end('get')
    }
    //响应
    //res.end('<h1>hi,user</h1>')
});
//4.监听3000端口
app.listen(3000);
console.log('服务器已启动，监听3000端口，请访问 localhost：3000');